<?php
//error_reporting(0);

/**
 * 话题推荐管理 
 * 
 * @copyright caijian
 * @since 2012-03-21
 */

include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/inc.php");

$g_cgival = array();
$g_pro = array();
$g_show = array();

function show_pro()
{
	global $g_show;
	$smarty = p_get_smarty ();
	$smarty->assign( $g_show );

	if (! empty($g_show['error']) ) {
		print $g_show['errmsg'];
		exit;
	} else {
		$smarty->display("foodv2/topic/topic_tuijian.html" );
	}
}


function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;
	cgi::both($g_cgival["tab"], "tab", 1);
	cgi::both($g_cgival["s_key"], "s_key", '');
	$g_cgival["s_key"] = string::un_script_code($g_cgival["s_key"]);
	$g_cgival["s_key"] = string::un_html($g_cgival["s_key"]);

	cgi::both($g_cgival["s_pos"], "s_pos", 1);
	cgi::both($g_cgival["s_date"], "s_date", '');
	$g_cgival["s_date"] = string::un_script_code($g_cgival["s_date"]);
	$g_cgival["s_date"] = string::un_html($g_cgival["s_date"]);


	cgi::both($g_cgival["tab"], "tab", 0);
	if ($g_cgival['tab']) $g_cgival['tab'] = 1;
}


function get_data()
{
	global $g_cgival, $g_pro, $g_show;
	global $config;
	
	
	// 拿到当前站点的site_id
	$site_id = admin::admin_get_user_site_id();
	$admin_id = admin::admin_get_login_uid();
	$admin_name = admin::admin_get_login_user_name();

	$topic_site_id = $site_id==900 ? 901 : $site_id;
	$topic_group_id = $config['food_site_group_id'][$topic_site_id];

	if (! $topic_group_id) {
		$g_show['error'] = 1;
		$g_show['errmsg'] = '未定义站点group_id，请联系管理员';
	}

	// 读取已推荐话题：
	$db_topic = new db_food_topic();

	$where = "site_id={$site_id}";
	if ($g_cgival['s_key']) $where .= " and topic_title like '%{$g_cgival['s_key']}%'";
	if ($g_cgival['tab'] ) $where .= ' and status=1';

	$tdata = $db_topic->admin_get_food_topic_list($where);
	$tuijian_data = array();

	// g_show data:
	$data = array();
	$tuijianed = array();

	foreach ($tdata as $t) {
		$tuijian_data[$t['id']] = $t;
		if ($t['status']) {
			$t['tj_position'] = $config['food_topic_tj_pos'][$t['topic_tj_pos']];
			$data[] = $t;
			$tuijianed[$t['id']] = $t;
		}
	}

	if (! $g_cgival['tab']) {

		// 通过接口读取话题：
		$topic_jsc = file_get_contents($config['food_topic_api'].$topic_group_id);
		$topic_data = json_decode($topic_jsc);

		foreach($topic_data->list as $t) {
			$tj = array(
				'id'	=>	$t->wgt_id
				,'topic_title'	=>	$t->topic
				,'topic_content'	=>	$t->intro
				,'topic_thumb'	=>	$t->thumb
				,'group_id'	=>	$t->group_id
				,'group_name'	=>	$t->group_name
		//		,'site_id'	=>	$t->site_id
				,'topic_status'	=>	$t->status
		//		,'create_time'	=>	$t->create_time
		//		,'update_time'	=>	$t->update_time
				,'admin_id'	=>	$admin_id
				,'admin_name'	=>	$admin_name
			);

			$tj['site_id'] = $topic_site_id;

	//		if ($tj['site_id'] != $topic_site_id) {
	//			$g_show['error'] = 1;
	//			$g_show['errmsg'] = '通过API接口获取到的site_id和当前site_id不一致，请联系管理员';
	//			return;
	//		}

			if (array_key_exists($tj['id'],$tuijianed) ) {
				$tj['status'] = 1;
			} else {
				$tj['status'] = 0;
				$data[] = $tj;

				if (! array_key_exists($tj['id'],$tuijian_data) ) {
					$db_topic->admin_insert_food_topic($tj);
				}
			}
		}
	}

	$g_show['data'] = $data;

	$g_show['tabs'] = array(0=>'全部话题',1=>'已推荐');
	$g_show['tab'] = $g_cgival['tab'];
	$g_show['s_key'] = $g_cgival['s_key'];


/*
	
	// 搜索
	$data = $db->admin_shangjia_search($site_id, $tag_class, $tag_name, $is_shangjia, $is_tj);
	$count = $db->admin_shangjia_search_count(site_id, $tag_class, $tag_name, $is_shangjia, $is_tj);
	
	if (is_array($data))
	{
		$g_show["data"] = $data;
		$g_show["count"] = $count;
	}
	else
	{
		$g_show["data_none"] = 1;
	}

	
	
	// Pager 
	$max_page = page_bar::get_page_max($g_show["count"], $g_cgival['pcount'], $g_cgival["p"]);
	
	$page_bar_style = array(
			"curt"		=> $g_cgival["p"],
			"max"		=> $max_page,
			"key"		=> "p",
	);
	
	$g_show["page_bar"] = "<b>{$g_show["count"]}</b> 条记录&nbsp;&nbsp;第{$g_cgival["p"]}页&nbsp;共{$max_page}页&nbsp;" . page_bar::bar2($page_bar_style);

*/
}

try
{
	admin_check_login();
// 	admin_check_user_priv("foodv2/main_nav.php");
	
	check_cgi_pro();
	get_data();
}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}

show_pro();
exit;


